package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.operate;
import cn.com.hfuu.module.operategz;
import cn.com.hfuu.module.operatern;
import cn.com.hfuu.module.operatesq;
import cn.com.hfuu.module.warehousetea;
import cn.com.hfuu.module.warehouseteax;
import cn.com.hfuu.service.GzService;
import cn.com.hfuu.service.OperateService;
import cn.com.hfuu.service.TeaService;
import cn.com.hfuu.service.TeaxService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.vo.Page;


@Controller
@RequestMapping("gz")
public class GzControl {
	
	@Autowired
	GzService gzService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	TeaxService teaxService;
	
	@Autowired
	TeaService teaService;
	
	@Autowired
	OperateService operateService;
	
	@RequestMapping("info")
	public String info(){
		return "operate/gz/gz";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page list(Integer start, Integer limit, operategz gz){
		List<operategz> list=gzService.selectPage(start,limit,gz);
		List<operategz> total=gzService.selectpagetotal(gz);
		for (operategz gz1 : list) {
			gz1.setUser(userService.selectuserbyid(gz1.getOperateUserid()));
			gz1.setWarehouseteax(teaxService.selectbyid(gz1.getOperateTeaxid()));
			gz1.toString();
		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(list );
		page.setTotal(total.size() );
		return page;
	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
 		List<operategz> list =gzService.selectpagetotal(new operategz());
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (operategz gz1 : list) {
			Map<String, String> map1 = new HashMap<String, String>();
			gz1.setUser(userService.selectuserbyid(gz1.getOperateUserid()));
			gz1.setWarehouseteax(teaxService.selectbyid(gz1.getOperateTeaxid()));
			
			map1.put("编号",gz1.getOperateGzid());
			map1.put("茶名",gz1.getWarehouseteax().getWarehouseTeaname());
 
			map1.put("用时（min）", gz1.getOperateTotaltime());
			map1.put("初烘温度（℃）", gz1.getOperateWd1());
			map1.put("足烘温度（℃）", gz1.getOperateWd2());
			map1.put("耗时(min)", gz1.getOperateTotaltime());
			map1.put("重量(克)", gz1.getOperateQuality()+"");
			map1.put("操作人", gz1.getUser().getUserName());
			map1.put("操作时间", gz1.getOperateTime());
			mapList.add(map1);

		}
		String[] title = { "编号", "茶名","初烘温度（℃）","足烘温度（℃）", "耗时(min)","重量(克)","操作人","操作时间" };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "茶叶干燥信息");
		return new ModelAndView(ve, map);
	}
	
	@RequestMapping("listad")
	@ResponseBody
	public List<operategz> listad(){

		List<operategz> total=gzService.selectpagetotal(new operategz());
		for (operategz gz1 : total) {
			gz1.setUser(userService.selectuserbyid(gz1.getOperateUserid()));
			gz1.setWarehouseteax(teaxService.selectbyid(gz1.getOperateTeaxid()));
			gz1.toString();
		}
	 
		return total;
	}
	
	@RequestMapping("edit")
	public ModelAndView edit(String id){
		operategz gz=gzService.selectbyid(id);
		warehouseteax teax =teaxService.selectbyid(gz.getOperateTeaxid());
		ModelAndView mv=new ModelAndView();
		mv.addObject("gz", gz);
		mv.addObject("teax", teax);
		mv.setViewName("operate/gz/edit");
		return mv;
		
	}
	
	@RequestMapping("editproc")
	@ResponseBody
	public String editproc(operategz gz){
		if(gzService.update(gz)>0){
			return "success";
		}
		return null;
		
	}
	
	/**
	 * 干燥删除
	 * 1、是否进库
	 * 2、删除库表
	 * 3、删除加工日志
	 * 4、更新加工
	 * @param id
	 * @return
	 */
	
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String id){
		operategz gz=gzService.selectbyid(id);
		operate op=operateService.selectbyid(gz.getOperateId());
		warehousetea tea=teaService.selectbyoperateid(op.getOperateId());
		if(tea.getState()==1){
			return "false";
		}else{
			op.setWarehouseState(2);
			op.setState(1);
			operateService.update(op);
			teaService.deletebyid(tea.getWarehouseTeaid());
			gzService.deletebyid(gz.getOperateGzid());
			return "success";
		}
		
	}
}
